<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" name="" id="userinp">
    <input type="password" name="" id="pwdinp">
    <button>register</button>
    <script>
    var btn=document.getElementsByTagName("button")[0]
    var user=document.getElementById("userinp")
    var pwd=document.getElementById("pwdinp")
    // if(document.cookie){
    //     var str=document.cookie
    //     var arr=str.split("; ")
    //     var u=arr[0].split("=")[1]
    //     var p=arr[1].split("=")[1]
    //     user.value=u
    //     pwd.value=p
    // }
    var date=new Date()
    date.setDate(date.getDate()+1)
    btn.onclick=function(){
        if(!getcookie("user_"+user.value)){
        setcookie("user_"+user.value,user.value,1);
        setcookie("pwd_"+user.value,pwd.value,1);
        location.href="login.html"
        // console.log(user.value,pwd.value)
        // document.cookie="user0="+user.value+";expires="+date
        // document.cookie="pwd0="+pwd.value+";expires="+date
    }
    else {
        alert("用户名重复")
    }}

    function setcookie(name,value,d,path="/"){
        var date=new Date()
        date.setDate(date.getDate()+d)
        document.cookie=name+"="+encodeURIComponent(value)+";expires="+date+";path="+path
    } 
    function getcookie(name){
      if(document.cookie){
        var str=document.cookie
        var arr=str.split(";")
        for(var i=0;i<arr.length;i++){
            var key=arr[i].trim().split("=")[0]
            var value=arr[i].trim().split("=")[1]
            if(key==name){
                return decodeURIComponent(value)
            }
        }   
      }
      return ""
    }
    </script>
</body>
</html>